<?php
/**
 * Author：ghostsf
 * Blog：www.ghostsf.com
 * Date: 2016/6/16
 */

namespace app\admin\controller;
use app\admin\model\Notice;
use app\admin\model\Campus;
use app\admin\validate\NoticeValidate;
use think\Db;

/**
 * 公告
 * Class NoticeController
 * Author mww
 * @package app\admin\controller
 */
class LunboController extends CommonController
{
    /**
     * 获得数据
     */
    public function getDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $notice = Db::table('ew_lunbo')->where($_POST)->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_lunbo')->where($_POST)->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json_encode($data);
    }
    /**
     * 显示添加页
     */
    public function addnotice()
    {
        $notice = Db::table('ew_notice')->where('n_type',2)->whereOr('n_type',3)
            ->field('n_id,n_title')->order('n_updatetime desc')->select();
        $project = Db::table('ew_project')->where('p_status',1)->whereOr('p_status',3)
            ->field('p_id,p_name')->order('p_updatetime desc')->select();
        $course = Db::table('ew_course')->where('c_status',2)
            ->field('c_id,c_name')->order('c_updatetime desc')->select();
        $this->assign("notice", $notice);
        $this->assign("project", $project);
        $this->assign("course", $course);
        return $this->fetch("config/add");
    }
    /**
     * 添加
     */
    public function insert()
    {
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        $data = array();
        $data['lb_name'] =$_POST['lb_name'];
        if(empty($_POST['lb_name'])){return json_encode(['status'=>0,'msg'=>'轮播图名字不能为空']);}
        $data['lb_type'] =$_POST['lb_type'];
        if($_POST['lb_type'] == 1){
            if(empty($_POST['lb_cid'])){return json_encode(['status'=>0,'msg'=>'课程不能为空']);}
            $data['lb_cid'] =$_POST['lb_cid'];
        }
        if($_POST['lb_type'] == 2){
            if(empty($_POST['lb_pid'])){return json_encode(['status'=>0,'msg'=>'项目不能为空']);}
            $data['lb_pid'] =$_POST['lb_pid'];
        }
        if($_POST['lb_type'] == 3){
            if(empty($_POST['lb_nid'])){return json_encode(['status'=>0,'msg'=>'公告不能为空']);}
            $data['lb_nid'] =$_POST['lb_nid'];
        }
        if(!empty($_FILES['pic']['tmp_name'])){
            if(move_uploaded_file($_FILES['pic']['tmp_name'],"{$dir}/nanyi/lunbo/{$_FILES['pic']['name']}")){
                $data['lb_pic'] = "{$url}/nanyi/lunbo/{$_FILES['pic']['name']}";
            }else{
                return json_encode(['status'=>0,'msg'=>'图片保存失败']);
            }
        }else{
            return json_encode(['status'=>0,'msg'=>'图片不能为空']);
        }
        $data['lb_createtime'] = date('Y-m-d H:i:s');
        $data['lb_updatetime'] = date('Y-m-d H:i:s');
        $res = Db::table('ew_lunbo')->insert($data);
        if($res){
            return json_encode(['status'=>1,'msg'=>'添加成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'添加失败']);
        }
    }
    /**
     * 显示修改页
     */
    public function updatenotice()
    {
        $lbid = $_POST['lbid'];
        $notice = Db::table('ew_notice')->where('n_type',2)->whereOr('n_type',3)
            ->field('n_id,n_title')->order('n_updatetime desc')->select();
        $project = Db::table('ew_project')->where('p_status',1)->whereOr('p_status',3)
            ->field('p_id,p_name')->order('p_updatetime desc')->select();
        $course = Db::table('ew_course')->where('c_status',2)
            ->field('c_id,c_name')->order('c_updatetime desc')->select();
        $data = Db::table('ew_lunbo')->where('lb_id',$lbid)->find();
        $this->assign("notice", $notice);
        $this->assign("project", $project);
        $this->assign("course", $course);
        $this->assign("data", $data);
        return $this->fetch("config/updateLunbo");
    }
    /**
     * 修改
     */
    public function update()
    {
        $lbid = $_POST['lb_id'];
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        $data = array();
        $data['lb_name'] =$_POST['lb_name'];
        if(empty($_POST['lb_name'])){return json_encode(['status'=>0,'msg'=>'轮播图名字不能为空']);}
        $data['lb_type'] =$_POST['lb_type'];
        if($_POST['lb_type'] == 1){
            if(empty($_POST['lb_cid'])){return json_encode(['status'=>0,'msg'=>'课程不能为空']);}
            $data['lb_cid'] =$_POST['lb_cid'];
        }
        if($_POST['lb_type'] == 2){
            if(empty($_POST['lb_pid'])){return json_encode(['status'=>0,'msg'=>'项目不能为空']);}
            $data['lb_pid'] =$_POST['lb_pid'];
        }
        if($_POST['lb_type'] == 3){
            if(empty($_POST['lb_nid'])){return json_encode(['status'=>0,'msg'=>'公告不能为空']);}
            $data['lb_nid'] =$_POST['lb_nid'];
        }
        if(!empty($_FILES['pic']['tmp_name'])){
            if(move_uploaded_file($_FILES['pic']['tmp_name'],"{$dir}/nanyi/lunbo/{$_FILES['pic']['name']}")){
                $data['lb_pic'] = "{$url}/nanyi/lunbo/{$_FILES['pic']['name']}";
            }else{
                return json_encode(['status'=>0,'msg'=>'图片保存失败']);
            }
        }
        $data['lb_updatetime'] = date('Y-m-d H:i:s');
        $res = Db::table('ew_lunbo')->where('lb_id',$lbid)->update($data);
        if($res){
            return json_encode(['status'=>1,'msg'=>'修改成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'修改失败']);
        }
    }
    /**
     * 删除
     */
    public function deleteByIDs()
    {
        $ids = input('ids');
        $ids = rtrim($ids, ",");
        $deleteinfo = Db::table('ew_lunbo')->where('lb_id','in',"$ids")->delete();
        if ($deleteinfo) {
            return json_encode(array("status" => 1, "msg" => "删除成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "删除失败！"));
        }
    }

    //=================广告位===================
    /**
     * 获得数据
     */
    public function advertgetDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $notice = Db::table('ew_advert')->where($_POST)->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_advert')->where($_POST)->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json_encode($data);
    }
    /**
     * 显示修改页
     */
    public function advertupdatenotice()
    {
        $atid = $_POST['atid'];
        $notice = Db::table('ew_notice')->where('n_type',2)->whereOr('n_type',3)
            ->field('n_id,n_title')->order('n_updatetime desc')->select();
        $project = Db::table('ew_project')->where('p_status',1)->whereOr('p_status',3)
            ->field('p_id,p_name')->order('p_updatetime desc')->select();
        $course = Db::table('ew_course')->where('c_status',2)
            ->field('c_id,c_name')->order('c_updatetime desc')->select();
        $data = Db::table('ew_advert')->where('at_id',$atid)->find();
        $this->assign("notice", $notice);
        $this->assign("project", $project);
        $this->assign("course", $course);
        $this->assign("data", $data);
        return $this->fetch("config/updateadvert");
    }
    /**
     * 修改
     */
    public function advertupdate()
    {
        $atid = $_POST['at_id'];
        $dir = $_SERVER['DOCUMENT_ROOT'];
        $url =  "http://".$_SERVER['HTTP_HOST'];
        $data = array();
        $data['at_name'] =$_POST['at_name'];
        if(empty($_POST['at_name'])){return json_encode(['status'=>0,'msg'=>'轮播图名字不能为空']);}
        $data['at_type'] =$_POST['at_type'];
        if($_POST['at_type'] == 1){
            if(empty($_POST['at_cid'])){return json_encode(['status'=>0,'msg'=>'课程不能为空']);}
            $data['at_cid'] =$_POST['at_cid'];
        }
        if($_POST['at_type'] == 2){
            if(empty($_POST['at_pid'])){return json_encode(['status'=>0,'msg'=>'项目不能为空']);}
            $data['at_pid'] =$_POST['at_pid'];
        }
        if($_POST['at_type'] == 3){
            if(empty($_POST['at_nid'])){return json_encode(['status'=>0,'msg'=>'公告不能为空']);}
            $data['at_nid'] =$_POST['at_nid'];
        }
        if($_POST['at_type'] == 4){
            if(empty($_POST['at_href'])){return json_encode(['status'=>0,'msg'=>'链接地址不能为空']);}
            $data['at_href'] =$_POST['at_href'];
        }
        if(!empty($_FILES['pic']['tmp_name'])){
            if(move_uploaded_file($_FILES['pic']['tmp_name'],"{$dir}/nanyi/lunbo/{$_FILES['pic']['name']}")){
                $data['at_pic'] = "{$url}/nanyi/lunbo/{$_FILES['pic']['name']}";
            }else{
                return json_encode(['status'=>0,'msg'=>'图片保存失败']);
            }
        }
        $data['at_updatetime'] = date('Y-m-d H:i:s');
        $res = Db::table('ew_advert')->where('at_id',$atid)->update($data);
        if($res){
            return json_encode(['status'=>1,'msg'=>'修改成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'修改失败']);
        }
    }

    //======================特长========================

    /**
     * 获得数据
     */
    public function goodatgetDatas()
    {
        $rows = $_POST['rows'];
        $page = $_POST['page'];
        $_POST = $this->getDataByCampusid($_POST);
        $notice = Db::table('ew_good')->where($_POST)->limit($rows * ($page - 1), $rows)->select();
        $total = Db::table('ew_good')->where($_POST)->count();
        $data['total'] = $total;
        $data['rows'] = $notice;
        return json_encode($data);
    }
    /**
     * 显示添加页
     */
    public function goodataddnotice()
    {
        return $this->fetch("config/addgoodat");
    }
    /**
     * 添加
     */
    public function goodatinsert()
    {
        $name = trim($_POST['name'],' ');
        if(empty($name)){
            return json_encode(['status'=>0,'msg'=>'特长名称不能为空']);
        }
        $res = Db::table('ew_good')->insert(['g_name'=>$name]);
        if($res){
            return json_encode(['status'=>1,'msg'=>'添加成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'添加失败']);
        }
    }
    /**
     * 显示修改页
     */
    public function goodatupdatenotice()
    {
        $gid = $_POST['gid'];
        $data = Db::table('ew_good')->where('g_id',$gid)->find();
        $this->assign("data", $data);
        return $this->fetch("config/updategoodat");
    }
    /**
     * 修改
     */
    public function goodatupdate()
    {
        $gid = $_POST['gid'];
        $name = trim($_POST['name'],' ');
        if(empty($name)){
            return json_encode(['status'=>0,'msg'=>'特长名称不能为空']);
        }
        $res = Db::table('ew_good')->where('g_id',$gid)->update(['g_name'=>$name]);
        if($res){
            return json_encode(['status'=>1,'msg'=>'修改成功']);
        }else{
            return json_encode(['status'=>0,'msg'=>'修改失败']);
        }
    }
    /**
     * 删除
     */
    public function goodatdeleteByIDs()
    {
        $ids = input('ids');
        $ids = rtrim($ids, ",");
        $deleteinfo = Db::table('ew_good')->where('g_id','in',"$ids")->delete();
        if ($deleteinfo) {
            return json_encode(array("status" => 1, "msg" => "删除成功！"));
        } else {
            return json_encode(array("status" => 0, "msg" => "删除失败！"));
        }
    }

}